class LoginController < ApplicationController
  before_filter :authorize, :except => :login

  def login
    session[:user_id]=nil
    if request.post?
      user = User.authenticate(params[:name],params[:password])
      if user
        session[:user_id] = user.id
        session[:is_admin] = user.is_admin
        session[:complete_name] = user.complete_name
        uri = session[:original_uri]
        if user.is_admin
          redirect_to(uri || {:controller => "admin/users", :action => "index"})
        else
          redirect_to(uri || {:controller => "system/magazines", :action => "index"})
        end
      else
        flash[:notice] = "Usu&aacute;rio e/ou senha incorretos. Favor digitar novamente.<br/><br/>"
      end
    end
  end

  def logout
    session[:user_id]=nil
    session[:is_admin]=nil
    session[:original_uri]=nil
    session[:complete_name]=nil
    flash[:notice] = "Usu&aacute;rio realizou logout.<br/><br/>"
    redirect_to(:controller => "login", :action => "login")
  end
end
